//: [Previous](@previous)
/*:
 
 #7 [Reverse Integer](https://leetcode.com/problems/reverse-integer/description/)
 
 Given a 32-bit signed integer, reverse digits of an integer.
 
 给定一个 32位 的整数，将其反转输出.
 
 Example 1:
 ```
 Input: 123
 Output: 321
 ```
 
 Example 2:
 ```
 Input: -123
 Output: -321
 ```
 
 Example 3:
 ```
 Input: 120
 Output: 21
 ```
 
 */
import Foundation

class Solution {
    func reverse(_ x: Int) -> Int {
        var x = x
        var result = 0
        if (abs(x) > Int32.max) {
            return 0
        }
        
        while x != 0 {
            result = result * 10 + x % 10
            x = x / 10
        }
        
        if (abs(result) > Int32.max) {
            return 0
        }
        
        return result
    }
}

print(Solution().reverse(123))
print(Solution().reverse(-123))
print(Solution().reverse(120))
print(Solution().reverse(1534236469))


//: [Next](@next)
